import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 * Sheet 1, exercise 1
 * A class that provides the gui. With that gui you can calculate the gdc.
 * 
 * @author Dennis Meyer, Sebastian Brodehl, Sebastian Schappert
 * @version 1.0.0.0
 *
 */
public class EuklidGui {

	private JFrame frame;
	private JTextField txtNumber1;
	private JTextField txtNumber2;
	private JButton btnCalcMinus;
	private JButton btnCalcModulo;
	private JButton btnCalcBoth;
	private JLabel lblGdcResultMinus;
	private JLabel lblRoundsResultMinus;
	private JLabel lblNewLabel;
	private JLabel lblRoundsMinus_1;
	private JLabel lblGdcModulo;
	private JLabel lblRoundsModulo;
	private JLabel lblGdcResultModulo;
	private JLabel lblRoundsResultModulo;

	/**

	 * Launch the application.

	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					EuklidGui window = new EuklidGui();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */

	public EuklidGui() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JLabel lblBitteGebenSie = new JLabel("Bitte geben Sie die erste Zahl ein: ");
		JLabel lblBitteGebenSie_1 = new JLabel("Bitte geben Sie die zweite Zahl ein: ");
		lblGdcResultMinus = new JLabel();
		lblRoundsResultMinus = new JLabel();
		lblNewLabel = new JLabel("Gdc Minus: ");
		lblRoundsMinus_1 = new JLabel("Rounds Minus: ");
		lblGdcModulo = new JLabel("Gdc Modulo: ");
		lblRoundsModulo = new JLabel("Rounds Modulo: ");
		lblGdcResultModulo = new JLabel();
		lblRoundsResultModulo = new JLabel();
		
		
		txtNumber1 = new JTextField();
		txtNumber1.setColumns(10);
		txtNumber1.getDocument().addDocumentListener(new DocumentListener() {

			@Override
			public void removeUpdate(DocumentEvent arg0) {
				changeButtonState();
			}

			@Override
			public void insertUpdate(DocumentEvent arg0) {
				changeButtonState();
			}

			@Override
			public void changedUpdate(DocumentEvent arg0) {
				changeButtonState();
			}

		});

		txtNumber2 = new JTextField();
		txtNumber2.getDocument().addDocumentListener(new DocumentListener() {

			@Override
			public void removeUpdate(DocumentEvent e) {
				changeButtonState();
			}

			@Override
			public void insertUpdate(DocumentEvent e) {
				changeButtonState();
			}

			@Override
			public void changedUpdate(DocumentEvent e) {
				changeButtonState();
			}

		});
		txtNumber2.setColumns(10);

		btnCalcBoth = new JButton("Calc Both");
		btnCalcBoth.setEnabled(false);

		btnCalcMinus = new JButton("Calc Minus");
		btnCalcMinus.setEnabled(false);
		
		btnCalcModulo = new JButton("Calc Modulo");
		btnCalcModulo.setEnabled(false);
	
		btnCalcModulo.addMouseListener(new MouseAdapter() 
		{
			@Override
			public void mouseClicked(MouseEvent arg0) 
			{
				lblGdcResultModulo.setText
					(String.valueOf(Euklid.calcGdc(Integer.parseInt(txtNumber1.getText()), Integer.parseInt(txtNumber2.getText()), false)));
				lblRoundsResultModulo.setText(String.valueOf(Euklid.rounds));
			}
		});
		
		btnCalcBoth.addMouseListener(new MouseAdapter() 
		{
			@Override
			public void mouseClicked(MouseEvent e) 
			{
				lblGdcResultMinus.setText
					(String.valueOf(Euklid.calcGdc(Integer.parseInt(txtNumber1.getText()), Integer.parseInt(txtNumber2.getText()), true)));
				lblRoundsResultMinus.setText(String.valueOf(Euklid.rounds));
				
				lblGdcResultModulo.setText
					(String.valueOf(Euklid.calcGdc(Integer.parseInt(txtNumber1.getText()), Integer.parseInt(txtNumber2.getText()), false)));

				lblRoundsResultModulo.setText(String.valueOf(Euklid.rounds));
			}
		});
		
		btnCalcMinus.addMouseListener(new MouseAdapter() 
		{
			@Override
			public void mouseClicked(MouseEvent arg0) 
			{
				lblGdcResultMinus.setText
					(String.valueOf(Euklid.calcGdc(Integer.parseInt(txtNumber1.getText()), Integer.parseInt(txtNumber2.getText()), true)));
				lblRoundsResultMinus.setText(String.valueOf(Euklid.rounds));
				
			}

		});

		
		
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap(100, Short.MAX_VALUE)
					.addComponent(btnCalcModulo)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnCalcMinus)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnCalcBoth))
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel)
								.addComponent(lblRoundsMinus_1))
							.addGap(18)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblGdcResultMinus)
								.addComponent(lblRoundsResultMinus))
							.addGap(53)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblRoundsModulo)
								.addComponent(lblGdcModulo))
							.addGap(18)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblRoundsResultModulo)
								.addComponent(lblGdcResultModulo)))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblBitteGebenSie_1)
								.addComponent(lblBitteGebenSie))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(txtNumber1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(txtNumber2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
					.addContainerGap(77, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblBitteGebenSie)
						.addComponent(txtNumber1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblBitteGebenSie_1)
						.addComponent(txtNumber2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(36)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(lblGdcResultMinus)
						.addComponent(lblGdcResultModulo)
						.addComponent(lblGdcModulo))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblRoundsResultMinus)
						.addComponent(lblRoundsMinus_1)
						.addComponent(lblRoundsResultModulo)
						.addComponent(lblRoundsModulo))
					.addGap(34)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnCalcBoth)
						.addComponent(btnCalcMinus)
						.addComponent(btnCalcModulo))
					.addContainerGap(73, Short.MAX_VALUE))
		);
		frame.getContentPane().setLayout(groupLayout);
	}

	/**
	 * Check if the given string can be parsed into an integer.
	 * @param text the given string number
	 * @return true if the given string can be parsed into an integer.
	 */
	private boolean isStringNumber(String text)
	{
		if (text == "")
			return false;
		try
		{
			Integer.parseInt(text);
			return true;
		}
		catch (NumberFormatException ex)
		{
			return false;
		}
	}

	/**
	 * Helpermethod. Check if the text is valid. If it's valid all calc buttons are enabled.
	 */
	private void changeButtonState()
	{
		btnCalcBoth.setEnabled(isStringNumber(txtNumber1.getText()) && isStringNumber(txtNumber2.getText()));
		btnCalcMinus.setEnabled(isStringNumber(txtNumber1.getText()) && isStringNumber(txtNumber2.getText()));
		btnCalcModulo.setEnabled(isStringNumber(txtNumber1.getText()) && isStringNumber(txtNumber2.getText()));
	}
}







